﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpRewardInfoWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_RewardInfoModel model = new EmpbasicService.Emp_RewardInfoModel();
        private EmpbasicService.Emp_RewardInfoModel oldmodel = new EmpbasicService.Emp_RewardInfoModel();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        public int ID;
        public int EmpID;
        public bool IsNew = false;
        public EmpRewardInfoWindow(int empID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增奖惩信息";
                model = new EmpbasicService.Emp_RewardInfoModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑奖惩信息";
                Client.GetEmp_RewardInfoModelCompleted += new EventHandler<EmpbasicService.GetEmp_RewardInfoModelCompletedEventArgs>(Client_GetEmp_RewardInfoModelCompleted);
                Client.GetEmp_RewardInfoModelAsync("(" + EmpID + ")", ID);
            }
        }

        void Client_GetEmp_RewardInfoModelCompleted(object sender, EmpbasicService.GetEmp_RewardInfoModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_RewardInfoModel
                {
                    RewardNo = model.RewardNo,
                    RewardName = model.RewardName,
                    RewardReason = model.RewardReason,
                    OtherRewardReason = model.OtherRewardReason,
                    OKDate = model.OKDate,
                    OKUnit = model.OKUnit
                };
            }
            this.DataContext = model;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;
                Client.AddEmp_RewardInfoModelCompleted += new EventHandler<EmpbasicService.AddEmp_RewardInfoModelCompletedEventArgs>(Client_AddEmp_RewardInfoModelCompleted);
                Client.AddEmp_RewardInfoModelAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.OKDate = Convert.ToDateTime(begin.Text);
                else
                    model.OKDate = null;
                Client.UpdateEmp_RewardInfoModelCompleted += new EventHandler<EmpbasicService.UpdateEmp_RewardInfoModelCompletedEventArgs>(Client_UpdateEmp_RewardInfoModelCompleted);
                Client.UpdateEmp_RewardInfoModelAsync(model);
            }

        }

        void Client_UpdateEmp_RewardInfoModelCompleted(object sender, EmpbasicService.UpdateEmp_RewardInfoModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_RewardInfoModelCompleted(object sender, EmpbasicService.AddEmp_RewardInfoModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("奖励情况子集", "", "", model.EmpBasicID, FangShi, "");
            }
            else
            {
                if (oldmodel.OKDate != model.OKDate)
                    insertsys.Insert("奖励情况子集", oldmodel.OKDate == null ? null : Convert.ToDateTime(oldmodel.OKDate).ToShortDateString(),
                        model.OKDate == null ? null : Convert.ToDateTime(model.OKDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "批准日期");
                if (oldmodel.RewardNo != model.RewardNo)
                    insertsys.Insert("奖励情况子集", oldmodel.RewardNo, model.RewardNo,
                        model.EmpBasicID, FangShi, "奖惩代码");
                if (oldmodel.RewardName != model.RewardName)
                    insertsys.Insert("奖励情况子集", oldmodel.RewardName, model.RewardName,
                        model.EmpBasicID, FangShi, "奖惩名称");

                if (oldmodel.RewardReason != model.RewardReason)
                    insertsys.Insert("奖励情况子集", oldmodel.RewardReason, model.RewardReason,
                        model.EmpBasicID, FangShi, "奖惩原因");
                if (oldmodel.OtherRewardReason != model.OtherRewardReason)
                    insertsys.Insert("奖励情况子集", oldmodel.OtherRewardReason, model.OtherRewardReason,
                        model.EmpBasicID, FangShi, "其他奖惩原因");
                if (oldmodel.OKUnit != model.OKUnit)
                    insertsys.Insert("奖励情况子集", oldmodel.OKUnit, model.OKUnit,
                        model.EmpBasicID, FangShi, "批准机关名称");
            }

        }
    }
}
